import requests
'''
first agent for translation
'''
from fastapi import FastAPI
from pydantic import BaseModel
from dotenv import load_dotenv
load_dotenv()
import os
siliconflow_key = os.getenv('siliconflow_key')
# from ..schemas.TranslationRequest import TranslationRequest
class TranslationRequest(BaseModel):
    source_text: str 
    source_detect: str = 'zn'
    target_detect: str = '阿拉伯文'
    translated_text: str = 'hello'
app = FastAPI(title="My FastAPI App", version="1.0.0")

@app.get("/")
async def read_root():
    return {"message": "Hello, I am translation agent!"}
@app.post("/translate")
async def translate(req: TranslationRequest):
    '''
    translate text to chinese， 写个agent与他进行交互
    '''
    # tencent/Hunyuan-MT-7B

    url = "https://api.siliconflow.cn/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {siliconflow_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "model": "tencent/Hunyuan-MT-7B",
        "messages": [
            {
                "role": "user",
                "content": "把用户输入的文本翻译成英文，不要额外解释和回应。"
            },
            {
                "role": "user",
                "content": f"{req.source_text}" # 用户的请求
            }
        ],
            "max_tokens": 8192,  # 按需添加
            "temperature": 0.5, # 按需添加
            "top_k": 20,        # 按需添加
            "top_p": 0.4,       # 按需添加
            "frequency_penalty": 0 # 按需添加
    }

    response = requests.post(url, headers=headers, json=payload)
    import json
    answer = response.text
    answer = json.loads(response.text)
    context = answer['choices'][0]['message']['content']
    # print(context)
    return {
        "source_detect": f'{req.source_detect}',
        "source_text": f'{req.source_text}',
        "target_detect": f'{req.target_detect}',
        "translated_text": f'{context}',
    }

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
    '''
    get item by id
    '''
    return {"item_id": item_id, "q": q}
# ____________________________________
